package personal.xjl._23java2studentdemo.mapper;

import org.apache.ibatis.annotations.*;
import personal.xjl._23java2studentdemo.model.Student;

import java.util.List;

@Mapper//定义下面的StudentMapper，创建一个Mybatis对象
public interface StudentMapper {
    @Select("select * from student")
    public List<Student> selectAll();

    //删除学生，慎用，用一个status标识是否删除
    @Delete("delete from student where id=#{id}")
    public int deleteById(int id);

    //新增学生
    @Insert("insert into student(name,password,sex,clazz,birthday) values (#{name},#{password},#{sex},#{clazz},#{birthday})")
    int insert(Student student);

    //修改
    @Update("update student set name=#{name}, password=#{password}, sex=#{sex}, clazz=#{clazz}, birthday=#{birthday} where id=#{id}")
    int update(Student student);

    @Select("select * from student where id = #{id}")
    Student selectById(int id);

    @Select("select * from student limit #{fromIndex},#{pageSize}")
    @Results({
            @Result(column = "id",property = "id",javaType = int.class),
            @Result(column = "id",property = "scores",javaType = List.class,
                    many = @Many(select = "personal.xjl._23java2studentdemo.mapper.ScoreMapper.selectByStudentId")
            )
    })
    List<Student> selectByPage(int fromIndex, int pageSize);

    @Select("select  * from student where name=#{username} and password=#{password}")
    Student selectByNameAndPwd(String username, String password);
}
